package com.parktech.sun.web.controller.management.sunhz;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.sunhz.IndustryCategory;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.sunhz.IndustryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.IOException;

/**
 * User: Think
 * Date: 11-1-15
 * Time: 下午2:35
 */
@Controller
@RequestMapping("/management/industry/categories")
public class IndustryCategoryController {


    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private IndustryService industryService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public IndustryCategoryController(IndustryService industryService) {
        this.industryService = industryService;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search(IndustrySearchForm industrySearchForm) {

        Page page = new Page(pageSize, industrySearchForm.getPageNo());

        ModelAndView modelAndView = new ModelAndView("management/sunhz/industryCategorySearch");

        return modelAndView.addObject(industryService.searchCategory(industrySearchForm.getTitle(), page));
    }

    @RequestMapping(value = "/search", method = RequestMethod.DELETE)
    public String delete(@RequestParam("industryCategoryIds") Long[] industryIds) {
        industryService.deleteAll(IndustryCategory.class, industryIds);
        return "redirect:search";
    }

    @ModelAttribute("industryCategory")
    public IndustryCategory populateModel(HttpServletRequest request) {
        String strId = request.getParameter("id");

        if (strId == null || strId.equals("-1")) {
            return new IndustryCategory();
        }
        return industryService.getByCategoryId(new Long(strId));
    }

    @RequestMapping(value = "/{industryCategoryId}", method = RequestMethod.GET)
    public String edit(@PathVariable Long industryCategoryId, Model model) {
        IndustryCategory industryCategory = industryService.getByCategoryId(industryCategoryId);
        model.addAttribute(industryCategory);
        return "management/sunhz/industryCategoryForm";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute(new IndustryCategory());
        return "management/sunhz/industryCategoryForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(@ModelAttribute @Valid IndustryCategory industryCategory, BindingResult result) throws IOException {

        if (result.hasErrors()) {
            return "management/sunhz/industryCategoryForm";
        }
        industryService.saveOrUpdateCategory(industryCategory);

        return "redirect:/management/industry/categories/search";
    }
}

